package com.nari.ysphd.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.jeremyfeinstein.okhttputil.callback.StringCallback;
import com.jeremyfeinstein.utils.Comm;
import com.jeremyfeinstein.utils.YCHShttpAPI;
import com.nari.ysphd.MyApp;
import com.nari.ysphd.R;
import com.jeremyfeinstein.utils.AppConstants;
import com.jeremyfeinstein.utils.ConfigUtil;
import com.nari.ysphd.util.inputFilter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class SettingIPActivity extends BaseActivity {

    private EditText server_ip, port_input;
    private Button enter, cancel;
    private ImageView back;
    private Context context;
    private Spinner resoSpinner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_ip);
        context = this;
        initView();
        initListener();
        initData();
    }


    protected void initView() {
        //应急可视化服务地址
        server_ip = (EditText) findViewById(R.id.server_ip_input);
        //应急可视化服务端口
        port_input = (EditText) findViewById(R.id.server_port_input);
        //分辨率
        resoSpinner = (Spinner) findViewById(R.id.resolution);

        //初始化分辨率下拉列表
        initResolution(this);

//        server_ip.setOnClickListener(this);
//        port_input.setOnClickListener(this);

        enter = (Button) findViewById(R.id.setting_ip_enter);
        cancel = (Button) findViewById(R.id.setting_ip_cancel);
        back = (ImageView) findViewById(R.id.setting_ip_back);

        server_ip.setText(ConfigUtil.getInstance().getRemoteUrl());
        port_input.setText(ConfigUtil.getInstance().getRemotePort());
    }

    protected void initListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                server_ip.setText("");
                port_input.setText("");

            }
        });

        server_ip.setFilters(new InputFilter[]{new inputFilter(SettingIPActivity.this,server_ip,2)});
        port_input.setFilters(new InputFilter[]{new inputFilter(SettingIPActivity.this,port_input,2)});
//        Comm.LegitimateInput(server_ip, 3);

        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ip = server_ip.getText().toString();
                String port = port_input.getText().toString();
                if (TextUtils.isEmpty(ip)) {
                    Toast.makeText(context, "请输入视频会商服务地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(port)) {
                    Toast.makeText(context, "请输入视频会商服务端口", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Comm.IsIP(ip)) {
                    Toast.makeText(context, "请输入规范ip地址", Toast.LENGTH_SHORT).show();
                    return;
                }

                Toast.makeText(context, "设置已完成", Toast.LENGTH_SHORT).show();

                if (!ip.equals(ConfigUtil.getInstance().getRemoteUrl()) || !port.equals(ConfigUtil.getInstance().getRemotePort())) {
                    ConfigUtil.getInstance().setRemoteUrl(ip);
                    ConfigUtil.getInstance().setRemotePort(port);
                    ConfigUtil.getInstance().setIpChangeFlag(true);
                } else {
                    ConfigUtil.getInstance().setIpChangeFlag(false);
                }

                AppConstants.SERVER_URL = AppConstants.REQUEST_MODE + ConfigUtil.getInstance().getRemoteUrl() + ":" + ConfigUtil.getInstance().getRemotePort();
                finish();
            }
        });
    }

    protected void initData() {

    }

    /* private AlertDialog dialog;

     private void getInput(final TextView textView, String title, final String str) {
         final View dialogView = LayoutInflater.from(SettingIPActivity.this)
                 .inflate(R.layout.dialog_customize, null);
         final EditText dialog_edittext = (EditText) dialogView.findViewById(R.id.edit_text);
         dialog_edittext.setText(str);
         dialog = new AlertDialog.Builder(SettingIPActivity.this)
                 .setView(dialogView)
                 .setTitle(title)
 //                .setMessage(title)
                 .setCancelable(false)
                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
 //                        EditText dialog_edittext = (EditText) dialogView.findViewById(R.id.edit_text);
                         textView.setText(dialog_edittext.getText().toString().trim());
                     }
                 })
                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                     }
                 })
                 .show();
     }

     @Override
     public void onClick(View v) {
         switch (v.getId()) {
             case R.id.server_ip_input:
                 getInput(server_ip, "请填写IP地址", server_ip.getText().toString().trim());
                 break;
             case R.id.server_port_input:
                 getInput(port_input, "请填写服务器端口地址", port_input.getText().toString().trim());
                 break;
         }
     }
 */
    private void initResolution(final Activity context) {
        List<String> list = new ArrayList<String>();
        list.add("高清(720)");  //1280x720
        list.add("标清(640)");  //640x480
        list.add("流畅(320)");  //320x240

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
        resoSpinner.setAdapter(adapter);
        SpinnerAdapter apsAdapter = resoSpinner.getAdapter(); // 得到SpinnerAdapter对象
        int k = apsAdapter.getCount();
        for (int i = 0; i < k; i++) {
            if (ConfigUtil.getInstance().getResolution()
                    .equals(apsAdapter.getItem(i).toString())) {
                resoSpinner.setSelection(i, true);// 默认选中项
                break;
            }
        }
        resoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            // parent： 为控件Spinner com.aspsine.swipetoloadlayout.view：显示文字的TextView position：下拉选项的位置从0开始
            @SuppressWarnings("unchecked")
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                // 获取Spinner控件的适配器
                ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent
                        .getAdapter();
                ConfigUtil.getInstance().setResolution(
                        adapter.getItem(position));
                resoSpinner.setSelection(position);
            }

            // 没有选中时的处理
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(context, "请选分辨率", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /*if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }*/
    }

}
